/*
 * P3DMActionListener.java Created on Dec 10, 2007 by M.C.Boulahiya
 */
package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;


/**
 * @author M.C.Boulahiya
 *
 */
public class P3DMActionListener implements ActionListener {
	private Partial3DMatcher parentFrame;
	private static String s = "/home/potier/Equipe/Maiza";
	private static String OBJDIR = s+"/DataBase/Potteries/ObjFiles";
	private static String OBJDIR2 = s+"/DataBase/NonPotteries/ObjFiles";

	/**
	 * @param matcher 
	 * 
	 */
	public P3DMActionListener(Partial3DMatcher p3DMatcher) {
		super();
		this.parentFrame = p3DMatcher;
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent event) {
		String command = event.getActionCommand();
		
		this.parentFrame.prepareActions();
		
		if (command.equalsIgnoreCase("Exit")) {
			// to quit the SemanticArchaeo app
			this.parentFrame.getSimpleUniverse().cleanup();
			System.exit(0);
		} else if (command.startsWith("Obj")) {
			// to load an OBJ file
			Object[] options = {"Poterie", "Autre"};
      int n = JOptionPane.showOptionDialog(this.parentFrame,
      				"Poterie ou Pas ?", "Charger un OBJ",
              JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
              null, options, options[0]);
    
      JFileChooser chooser = null;
      if (n == JOptionPane.YES_OPTION) {
      	chooser = new JFileChooser(OBJDIR);
				chooser.setSelectedFile(new File("ScanD27b-010ko.obj")); 
      } else { //(n == JOptionPane.NO_OPTION)
      	chooser = new JFileChooser(OBJDIR2);
				chooser.setSelectedFile(new File("Gourd.obj"));
      } 
			
      OBJFileFilter objFileFilter = new OBJFileFilter();
			chooser.setFileFilter(objFileFilter);
			if (chooser.showOpenDialog(parentFrame) == JFileChooser.APPROVE_OPTION) {
				String filename = chooser.getSelectedFile().getAbsolutePath();
				if (command.equalsIgnoreCase("Obj ancient load"))
					this.parentFrame.fileObjToGraph(filename, false);
				else // (command.equalsIgnoreCase("Obj quick load"))
					this.parentFrame.fileObjToGraph(filename, true);
			} else
				System.err.println("The Load command was cancelled by the user.");
		}		
		
		this.parentFrame.finishActions();
	}

	

/**
 * This class permits to filters the files in the FileChooser.
 * TODO what about using an OBJFileChooser or a SemanticArchaeoFileChooser??
 * 
 * @author M.C.Boulahiya
 */
class OBJFileFilter extends FileFilter {
	public final static String obj = "obj";
	
	/**
	 * Default Constructor.
	 */
	public OBJFileFilter() {
		super();
	}

	
	/**
	 * Decides of the extentions that have to be accepted. Here only .obj files 
	 * are accepted.
	 * 
	 * @param f The file tested.
	 * @return the result of the test.
	 * 
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
	 */
	public boolean accept(File f) {
		if (f.isDirectory())
			return true;
		
		String extension = getExtension(f);

		if (extension.equals(obj)) 
    	return true;
    else
    	return false;
	}

	
	/**
	 * Gives the FileFilter description.
	 * 
	 * @return the String describing the FileFilter.
	 * 
* @see javax.swing.filechooser.FileFilter#getDescription()
	 */
	public String getDescription() {
		return "Just OBJ files";
	}
	
	
  /*
   * Get the extension of a file.
   */
  private String getExtension(File f) {
      String ext = null;
      String s = f.getName();
      if (s.contains(".")) {
      	int i = s.lastIndexOf('.');

        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        
        return ext;
      }
      else 
      	return "a";
      
  } // End of getExtension(File)

} // End of class OBJFileFilter

}// End of class P3DMActionListener

// End of file P3DMActionListener.java